using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace JointEffort_Gallery
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Screens : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public enum ScreenStates { TitleScreen, GameScreen, OptionScreen, HallOfFameScreen };
        private ScreenStates mainScreenState;
        public ScreenStates MainScreenState
        {
            get { return mainScreenState; }
            set { mainScreenState = value; }
        }
        private ScreenStates selection;
        public ScreenStates Selection
        {
            get { return selection; }
            set { selection = value; }
        }
        Game g;
        SpriteBatch sb;
        SpriteFont sf;
        TitleScreen titleScreen;
        OptionsScreen optionScreen;
        GameScreen gameScreen;
        HallOfFameScreen hallOfFameScreen;
        double gameLength;
        private Vector2 trueMiddle;
        public Vector2 TrueMiddle
        {
            get { return trueMiddle; }
        }
        KeyboardState prevKeyboard, keyboard;
        MouseState prevMouse, mouse;

        public Screens(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            mainScreenState = ScreenStates.TitleScreen;
            //gameLength = 3;
            trueMiddle = new Vector2(game.Window.ClientBounds.Width / 2, game.Window.ClientBounds.Height / 2);
            g = game;
            selection = ScreenStates.TitleScreen;
            keyboard = Keyboard.GetState();
            mouse = Mouse.GetState();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            titleScreen = new TitleScreen(this.g);
            optionScreen = new OptionsScreen(this.g);
            gameScreen = new GameScreen(this.g);
            hallOfFameScreen = new HallOfFameScreen(this.g);
            this.Game.Components.Add(titleScreen);
        }

        protected override void LoadContent()
        {
           // base.LoadContent();
           /* switch (mainScreenState)
            {
                case (ScreenStates.TitleScreen):
                    {
                        //titleScreen.LoadContent();
                        break;
                    }
                case (ScreenStates.OptionScreen):
                    {
                        
                        break;
                    }
                case (ScreenStates.GameScreen):
                    {
                        
                        break;
                    }
                case (ScreenStates.HallOfFameScreen):
                    {
                        
                        break;
                    }
            }*/
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            keyboard = Keyboard.GetState();
            mouse = Mouse.GetState();
            switch (mainScreenState)
            {
                case (ScreenStates.TitleScreen):
                {
                    switch (selection)
                    {
                        case (ScreenStates.GameScreen):
                            {
                                mainScreenState = ScreenStates.GameScreen;
                                this.g.Components.Remove(titleScreen);
                                this.g.Components.Add(gameScreen);
                            }
                            break;
                        case (ScreenStates.OptionScreen):
                            {
                                mainScreenState = ScreenStates.OptionScreen;
                                this.g.Components.Remove(titleScreen);
                                this.g.Components.Add(optionScreen);
                            }
                            break;
                        case (ScreenStates.HallOfFameScreen):
                            {
                                mainScreenState = ScreenStates.HallOfFameScreen;
                                this.g.Components.Remove(titleScreen);
                                this.g.Components.Add(hallOfFameScreen);
                            }
                            break;    
                    }
                    break;
                }
                case (ScreenStates.OptionScreen): {
                    if (gameLength > 4.0f)
                    {
                        mainScreenState = ScreenStates.GameScreen;
                        this.g.Components.Remove(optionScreen);
                        this.g.Components.Add(gameScreen);
                    } 
                    break;
                }
                case (ScreenStates.GameScreen): {
                    if (gameLength > 6.0f)
                    {
                        mainScreenState = ScreenStates.HallOfFameScreen;
                        this.g.Components.Remove(gameScreen);
                        this.g.Components.Add(hallOfFameScreen);
                    } 
                    break;
                }
                case (ScreenStates.HallOfFameScreen):
                    {
                    if (gameLength > 8.0f)
                    {
                        mainScreenState = ScreenStates.TitleScreen;
                        this.g.Components.Remove(hallOfFameScreen);
                    } 
                    break;
                }
            }
            gameLength += (gameTime.ElapsedGameTime.Milliseconds/1000.0f);
            prevMouse = mouse;
            prevKeyboard = keyboard;
           // base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //Get services required to draw to screen and fonts
            sb = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            sf = Game.Services.GetService(typeof(SpriteFont)) as SpriteFont;

            switch (mainScreenState)
            {
                case (ScreenStates.TitleScreen): {
                    //PREPAPRE!!!
                    sb.GraphicsDevice.Clear(Color.Red);
                    break; }
                case (ScreenStates.OptionScreen): {
                    //FOR THE!!!!
                    sb.GraphicsDevice.Clear(Color.Orange);
                    break; }
                case (ScreenStates.GameScreen): {
                    //JAPANESE!!!
                    sb.GraphicsDevice.Clear(Color.Yellow);
                    break; }
                case (ScreenStates.HallOfFameScreen): {
                    //SEIZURE ROBOTS!!!
                    sb.GraphicsDevice.Clear(Color.Green);
                    break; }
            }
            
            //Debug tool
            //Display title of current screen and total running game time despite which screen is being drawn.
            sb.Begin();
            sb.DrawString(sf, mainScreenState.ToString(), Vector2.Zero, Color.White);
            sb.DrawString(sf, "Game Time: " + gameLength.ToString() + " sec", new Vector2(0,15), Color.White);
            sb.End();
        }
    }
}
